#ifndef UI_FANFOU_FRAME_H
#define UI_FANFOU_FRAME_H

#include "fanfou_global.h"
#include "ToolbarFrame.h"
#include "TweetFrame.h"
#include "TweetView.h"
#include "DMFrame.h"
#include "UserViewerFrame.h"
#include <QPropertyAnimation>
#include <QNetworkRequest>
#include <QButtonGroup>
#include <QPushButton>
#include <QByteArray>
#include <QScrollBar>
#include <QObject>
#include <QMovie>
#include <QLabel>
#include <QTimer>
namespace Fanfou
{
	class FanfouDM;
	class TweetRepository;
	class FanfouContext;
	class FanfouContextPrivate;
	class FanfouFrame : public QFrame
	{
		Q_OBJECT
		public:
			FanfouFrame( FanfouContext*,FanfouContextPrivate* );
			~FanfouFrame();
			void showLoading();
			void showError();
			void init();

			void reduceWarningCount(ToolbarButton b);
			void setWarningCount(ToolbarButton b,int c);
			void setWarningCount(TweetView*, int c);

			void setMyselfIcon(const QPixmap& icon) { m_toolbar.m_mySelfBtn.setPixmap(icon); }
			void setLoadingView(ToolbarButton b, bool t) { t ? m_toolbar.showLoading(b) : m_toolbar.hideLoading(b); }
			void switchToView(MiniblogView v);
			void showUserDetail(const FanfouUser& user) { m_userViewer.showUserDetail(user); }
			void showUserDetail(const FanfouId& id) { m_userViewer.showUserDetail(id); }

			void cleanup();

			bool eventFilter(QObject* , QEvent* );
		public slots:
			void updateTimeline(const QList<FanfouTweet>&,const QVector<FanfouId>&);
			void updateMention(const QList<FanfouTweet>&,const QVector<FanfouId>&);

			void updateDM(const QList<FanfouDM*>&,const QVector<FanfouId>& deletedId);
			void loadOldTimeline();
			void loadOldMention();
		protected:
			void resizeEvent(QResizeEvent*);
		private:
			FanfouContext*	m_context;
			FanfouContextPrivate*	m_privateContext;
			MiniblogView	e_currentView;

			// UIs
			TweetView		m_timelineWidget;
			TweetView		m_mentionWidget;
			QWidget			m_dmWidget;

			ToolbarFrame		m_toolbar;
			UserViewerFrame	m_userViewer;
			QScrollBar		m_scrollBar;
			QWidget*		m_scrollTarget;
			// Loadings
			QWidget*		m_errorWidget;
			QLabel*			m_errorLabel;
			QPushButton*	m_errorBtn;

			QPropertyAnimation		m_animation;
			QPropertyAnimation		m_animation2;
			QTimer t_updateTimeInfoTimer;

			QList<TweetFrame*>	m_recycleTweetFrames;
			QHash<FanfouId,DMFrame*> m_dmFrames;
			QList<DMFrame*> m_dmFrameList;

			QWidget*	widgetForView(MiniblogView v);
			void updateScrollBar();
			void recyleFrame(TweetFrame* f) { m_recycleTweetFrames.append(f); }
			TweetFrame* getTweetFrame() { return m_recycleTweetFrames.length() > 0 ? m_recycleTweetFrames.takeLast():new TweetFrame(m_context); }
		private slots:
			void scrollView(int value) { m_scrollTarget->move(m_scrollTarget->x(),-value); }
			void toolbarButtonClicked(int);
			void animationFinished();
			void updateTimeInfo();

			void dmRemoved(FanfouDM*);
		friend class TweetView;
	};
} // namespace Sina

#endif // SINAFRAME_H
